<template>

  <div class="personal-main">
    <div class="personal-pay">
      <h3>
        <i>个人信息</i>
      </h3>
    </div>
    <div>
      <el-form label-width="80px">


        <el-form-item label="用户类型">

          <el-tag v-if="userInfo.userType===1">投资人</el-tag>

          <el-tag v-if="userInfo.userType===2">借款人</el-tag>
        </el-form-item>

        <el-form-item label="用户昵称">
          <el-input v-model="userInfo.nickName"></el-input>
        </el-form-item>

        <el-form-item label="身份证号">
          <el-input v-model="userInfo.idCard"></el-input>
        </el-form-item>

        <el-form-item label="邮箱">
          <el-input v-model="userInfo.email"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-upload
              :data="{ module: 'headImg' }"
              :limit="1"
              :multiple="false"
              :on-remove="onUploadRemoveHeadImg"
              :on-success="onUploadSuccessHeadImg"
              action="http://localhost:8888/api/file/upload"
              list-type="picture-card"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-button type="primary" @click="save()">确认</el-button>
      </el-form>
    </div>
  </div>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<script>
import cookie from "js-cookie";

export default {
  data() {
    return {
      userInfo: {},
      imageUrl: ''
    };
  },
  methods: {
    onUploadSuccessHeadImg(response, file) {
      this.userInfo.headImg = response.data.url;
    },
    onUploadRemoveHeadImg() {
      this.$message.error("头像上传失败");
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    save() {
      this.$axios.$put("/userInfo/updateUserInfo", this.userInfo)
          .then(response => {
            this.$message.success(response.message);
            console.log("这是cookie", this.userInfo);
            cookie.set('userInfo', this.userInfo);
            window.location.href = "/user/userInfo"

          })
    },
    fetchData() {
      this.$axios.$get("/userInfo/selectById")
          .then(response => {
            this.userInfo = response.data.userInfo;
            console.log(this.userInfo);
          })
    }

  },
  mounted() {
    this.fetchData();
  }
}
</script>
